/**
 * version 1.0 release 20/12/2014
 * 
 * @author Haric Silver
 * @email haric.silver@gmail.com
 */
(function($) {
	$.fn.openDialog = function(options) {
		if (typeof options === 'undefined') {
			options = {};
		}

		this.click(function(e) {
			e.preventDefault();

			$.openDialog(options);
		});

		return this;
	};

	/* create button confirm */
	function createBtn(settings) {
		var btnHtml = "<button class='confirm'>" + settings.confirmText
				+ "</button>";
		if (!settings.warning) {
			btnHtml += "<button class='cancel'>" + settings.cancelText
					+ "</button>";
		}
		return btnHtml;
	};

	$.openDialog = function(options) {
		var defaults = {
			title : '',
			text : 'Bạn có muốn tiếp tục?',
			confirmText : 'Có',
			cancelText : 'Không',
			warning : false,
			modal: false,
			width: '500px',
			duration: 900
		};
		var settings = $.extend({}, defaults, options);

		var modelHtml = "<div class='over'></div><div class='dialog'><div class='dg_title'><button class='dg_close'>x</button><span>"
				+ settings.title
				+ "</span></div><div class='dg_content'><p>"
				+ settings.text
				+ "</p></div><div class='dg_confirm'>"
				+ createBtn(settings) + "</div></div>";

		// add dialog into DOM
		$('body').append(modelHtml);
		
		// format dialog is center
		$('.dialog').css('width', settings.width);
		$('.dialog').css('margin-left', $('.dialog').width()/-2+'px');
		
		// set border background
		$('.dialog').css('border', settings.border);
		$('.dg_title').css('background-color', settings.color);
		
		// animate fade in down
		var h = $(window).height()/3 - $('.dialog').height()/2;
		if (h<=0) h=9;
		$('.over').fadeIn(300);
		$('.dialog').animate({top:h,opacity:1}, settings.duration, 'swing', function() {});
		
		// default focus
		if (settings.warning) {
			$('.dg_confirm .confirm').focus();
		} else {
			$('.dg_confirm .cancel').focus();
		}
		
		// set default close function
		var modal;
		if (settings.modal) {
			modal = $('.dg_close, .confirm, .cancel');
		} else {
			modal = $('.over, .dg_close, .confirm, .cancel');
			
			// set options functions
			$('.over').on('click', options.close);
		}
		
		// close dialog function
		modal.on('click', function() {
			$('.dialog, .over').clearQueue().fadeOut(300, function() {
				$(this).remove();
			});
		});
			
		// set options function
		$('.confirm').on('click', options.confirm);
		$('.cancel').on('click', options.cancel);
		$('.dg_close').on('click', options.close);
	};
})(jQuery);